<?php
class activityViewAdmin extends coreFrameworkView
{
	function __construct()
	{
		parent::__construct();
		
		
		
	}

	function lottery(){
		
		$where['status'] = 1;
		$applist = $this->model->getLottery();	
		
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_activity/lottery.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	
	function lottery_form(){
		
		$get = $this->GVar->fget;
		if ($get['id']) {
			$data = $this->model->getLotteryRow($get['id']);
			$this->tmp->assign('data',$data);
		}
		$this->tmp->assign("main_show_block","admin/com_activity/lottery_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	function lottery_save(){
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$post['c_time'] = time();
		
		$res = $this->model->saveLottery($post,$id);
		$url = "index.php?m=admin&o=activity&t=lottery";
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
	}
	function lottery_del(){
		
		$id = $this->GVar->fget['id'];
	
		
		$res = $this->model->delLottery($id);
		$url = "index.php?m=admin&o=activity&t=lottery";
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
	}
	function excel(){      
		
		
		if($_FILES['excel']){
			
	    	include _SITE_INCLUDE_CLASS_PATH."upfile.php";
	    	
       		$upfile=new upload();
       		
       		
		    $file = $upfile->upFile($_FILES['excel']);
		}
		
		
	   
		if ($file){
			require( _SITE_INCLUDE_CLASS_PATH.'/phpexcel/PHPExcel.php');
		
			$reader = new PHPExcel_Reader_Excel2007();
			
			require_once _SITE_INCLUDE_CLASS_PATH.'/phpexcel/PHPExcel/IOFactory.php';
			chmod($file,0777);
            $PHPExcel = PHPExcel_IOFactory::load($file);
		
			//$file =  _SITE_INCLUDE_CLASS_PATH.'phpexcel/demo.xls';
		    
			//$PHPExcel = new PHPExcel();
			 //var_dump($PHPExcel);
	       
			$currentSheet = $PHPExcel->getActiveSheet();        //**读取excel文件中的指定工作表*/
			$allColumn = $currentSheet->getHighestColumn();        //**取得最大的列号*/
			$allRow = $currentSheet->getHighestRow();  
			$data=array();
			
			
			for($rowIndex=2;$rowIndex<=$allRow;$rowIndex++){        //循环读取每个单元格的内容。注意行从1开始，列从A开始
					for($colIndex='A';$colIndex<=$allColumn;$colIndex++){
							$addr = $colIndex.$rowIndex;
							$cell = $currentSheet->getCell($addr)->getValue();
							if($cell instanceof PHPExcel_RichText){ //富文本转换字符串
									$cell = $cell->__toString();
							}
							$data[$rowIndex][$colIndex] = $cell;
					}
			}
			
		
			foreach ($data as $v){
			    
    				$arr = array(
    					'num'=>$v['A'],
    					'name'=>$v['B'],
    					'mobile'=>$v['C'],
    					'content'=>$v['D'],
    					'c_time'=>time(),
    				
    				);
    				$this->model->saveLottery($arr);
			    
				
			}
		}
		MessageClass::ShowMessage(_lang_save_success,$url,1,_lang_save_fail,$url,3);
    }  
	function bus_manage_add()
	{
		$business_data = $this->model->getAllChannel();
		$this->tmp->assign('group',$business_data);
		$this->tmp->assign("main_show_block","admin/com_ticket/bus_manage_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//保存商家信息
	function bus_manage_save()
	{
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$post['c_time'] = time();
		$post['key_base_64'] = base64_encode($post['business_key']);
		$res = $this->model->saveBusiness($post,$id);
		$url = "index.php?m=admin&o=ticket&t=bus_manage";
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
	}
//修改商家
	function bus_manage_edit()
	{
		$id = $this->GVar->get['id'];
		$data = $this->model->selectBussessionById($id);
		$business_data = $this->model->getAllChannel();
		$this->tmp->assign('group',$business_data);
		$this->tmp->assign('data',$data);
		$this->tmp->assign("main_show_block","admin/com_ticket/bus_manage_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//删除商家
	function bus_manage_del()
	{
		$get=$this->GVar->fget;
		$data = array(
			'status'=>0
		);
		$resources_data = $this->model->delBusiness_manageById($data,$get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=ticket&t=bus_manage',1,_lang_save_fail,'index.php?m=admin&o=ticket&t=bus_manage',2);
	}

	//ajax创建发票
	function ajaxCreateTicket(){
		$post = $this->GVar->fpost;
		
		foreach ($post['count'] as $k=>$v){
			$price += $v*$post['unitPrice'][$k];
		}
		
		$number = date('YmdHis').rand(1000,9999);
		$data = array(
			'price'=>$price,
			'shop_id'=>$post['shop_id'],
			'c_time'=>time(),
			'number'=>$number,
			'create_user'=>$this->GVar->session['user_root']['id'],
			'status'=>1,
		);
		$res = $this->model->createTicket($data);
		
		
		if ($res) {
			
			foreach ($post['wareName'] as $k=>$v){
				$insert_data[] = array(
					'wareName'=>$v,
					'count'=>$post['count'][$k],
					'unit'=>$post['unit'][$k],
					'unitPrice'=>$post['unitPrice'][$k],
					'taxRate'=>$post['taxRate'][$k],
					'ticket_id'=>$res,
					'c_time'=>time(),
				
				);
				
			}
			$res = $this->model->insertTicketGoods($insert_data);
			echo trim($number);
		}else{
			echo 0;
		}
		
	}
	//冲红
	function revokeTicket()
	{		
		$url = getenv("HTTP_REFERER");
		$get = $this->GVar->fget;
		//引入发票类
		require( _SITE_INCLUDE_CLASS_PATH.'/fapiao.Class.php');
		//参数配置
		$ticket_info = $this->model->getTicketInfo($get['number']);
		$config = array(
			'host' =>'42.159.205.189',
			'port' =>'8301',
			'taxpayerId' =>$ticket_info['taxpayerId'],
			'taxpayerKey' =>$ticket_info['taxpayerKey'],
			'buiness_name'=>$ticket_info['business_name']
		);
		//实例化发票类
		$fapiao_class = new fapiao($config);
		$ticket_query_info = $this->model->getTicketQuery($ticket_info['requestCode']);
		if (!$ticket_query_info) {
			
			$ticket_query_info = $fapiao_class->query(array('requestCode'=>$ticket_info['requestCode']));
			if ($ticket_query_info['code']=='1111'){
				$msg = '发票未开具';
				MessageClass::ShowMessage($msg,$url,1,_lang_save_fail,$url,3);
			}elseif($ticket_query_info['code']=='2222'){
				
				$msg = '发票开具中';
				MessageClass::ShowMessage($msg,$url,1,_lang_save_fail,$url,3);
			}
		}
		
		$orderNo = date('YmdHis').rand(1000,9999);
		$post_data = array(
			'replacedInvoiceCode'=>$ticket_query_info['invoiceCode'],
			'replacedInvoiceNo'=>$ticket_query_info['invoiceNo'],
			'revokeReson'=>$get['revokeReson'],
			'orderNo'=>$orderNo,
		);
		
		
		
		$revoke = $fapiao_class->revoke($post_data);
		
		
		if ($revoke['code']=='0000') {
			$update_data = array(
				'revoke_requestCode'=>$revoke['requestCode'],
				'revoke_orderNo'=>$orderNo,
				'revoke_reson'=>$get['revokeReson'],
				'status'=>3,
				'revoke_time'=>time(),
			
			);
			
			
			$where[] = 'number='.$get['number'];
			$res = $this->model->updateTicket($update_data,$where);
		}else{
			$res = 0;
		}
		
		
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
		
		
	}
	//开票统计
	function statistics(){
		$get = $this->GVar->fget;
		$business_info = $this->model->selectBussessionById($get['id']);
		//引入发票类
		require( _SITE_INCLUDE_CLASS_PATH.'/fapiao.Class.php');
		$config = array(
			'host' =>'42.159.205.189',
			'port' =>'8301',
			'taxpayerId' =>$business_info['taxpayerId'],
			'taxpayerKey' =>$business_info['taxpayerKey'],
			'buiness_name'=>$business_info['business_name']
		);
	
		
		//实例化发票类
		$fapiao_class = new fapiao($config);
		
	
		
		//开票统计
		if ($get['year']){
			$post_data['queryDate'] = $get['year'].$get['month'];
		}else{
			$post_data['queryDate'] = date('Ym');
		}
		
		$querybillingcount = $fapiao_class->querybillingcount($post_data);
//		print_r('<pre>***');
//		print_r($querybillingcount);
//		print_r('</pre>');
		$this->tmp->assign('tongji',$querybillingcount);
		//余票查询
		$queryBillLeftCount = $fapiao_class->queryBillLeftCount();
		for ($i=date('Y');$i>=date('Y')-10;$i--){
			
			$year[] = $i;
			
		}
		for ($i=1;$i<13;$i++){
			
			$month[] = $i;
			
		}
		$this->tmp->assign('year',$year);
		$this->tmp->assign('month',$month);
		$this->tmp->assign('lelf_count',$queryBillLeftCount);
		$this->tmp->assign("main_show_block","admin/com_ticket/business_statistics.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//查看发票
	function queryTicket(){
		$url = getenv("HTTP_REFERER");
		$get = $this->GVar->fget;
		//引入发票类
		require( _SITE_INCLUDE_CLASS_PATH.'/fapiao.Class.php');
		//参数配置
		$ticket_info = $this->model->getTicketInfo($get['number']);
	
		$this->tmp->assign('ticket',$ticket_info);
		$config = array(
			'host' =>'42.159.205.189',
			'port' =>'8301',
			'taxpayerId' =>$ticket_info['taxpayerId'],
			'taxpayerKey' =>$ticket_info['taxpayerKey'],
			'buiness_name'=>$ticket_info['business_name']
		);
		//实例化发票类
		$fapiao_class = new fapiao($config);
		$ticket_query_info = $this->model->getTicketQuery($ticket_info['requestCode']);
		
		if (!$ticket_query_info) {
			
			$ticket_query_info = $fapiao_class->query(array('requestCode'=>$ticket_info['requestCode']));
			
			if ($ticket_query_info['code']=='0000'){
				
				unset($ticket_query_info['items']);
				//图片地址
				$images = $ticket_query_info['imageFiles'];
				unset($ticket_query_info['imageFiles']);
				$ticket_query_info['issueDate'] = strtotime($ticket_query_info['issueDate']);
				$ticket_query_info['c_time'] = time();
				$ticket_query_info['true_pdfFile'] = $ticket_query_info['pdfFile'];
			
				$query_id = $this->model->saveTicketQuery($ticket_query_info);
				
				if ($query_id) {
					foreach ($images as $v){
						$insert_data[] = array(
							'q_id'=>$query_id,
							'imageFiles'=>$v,
							'true_imageFiles'=>$v,
							'c_time'=>time(),
						);
					}
					$this->model->saveTicketQueryImages($insert_data,$query_id);
				}
				$ticket_query_info['imageFiles'] = $insert_data;

				$update_data['query_status'] = 1;
				$this->model->updateTicket($update_data,array('requestCode'=>$ticket_info['requestCode']));
				
			}
			
			if ($ticket_query_info['code']=='1111'){
				$msg = '发票未开具';
				//MessageClass::ShowMessage($msg,$url,1,_lang_save_fail,$url,3);
			}elseif($ticket_query_info['code']=='2222'){
				
				$msg = '发票开具中';
				//MessageClass::ShowMessage($msg,$url,1,_lang_save_fail,$url,3);
			}
		}else{
			
			
			$ticket_query_info['imageFiles'] = $this->model->getTicketQueryImages($ticket_query_info['id']);
		}
			//$this->tmp->assign('data',$ticket_query_info);
			
		
		
		
		$this->tmp->assign('data',$ticket_query_info);
		$this->dp("admin/com_ticket/ticket_query");
		
		
		
		
	}
	//创建发票
	function createTicket(){
		$id = $this->GVar->get['id'];
		$data = $this->model->selectBussessionById($id);
		
		
	
		$this->tmp->assign('data',$data);
		$this->tmp->assign("main_show_block","admin/com_ticket/create_ticket.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	
	
//发票记录
	function bus_manage_log_list()
	{		
		$id = $this->GVar->fget['id'];
		$applist = $this->model->getAllTicket($id);
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_ticket/bus_manage_log_list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//渠道方列表
	function channel()
	{
		$applist = $this->model->getAllChannel();
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//编辑渠道方
	function channel_edit()
	{
		$id = $this->GVar->get['id'];
		$data = $this->model->getChannelById($id);
		$user_data = $this->model->getChannelUserById($id);
		$this->tmp->assign('data',$data);
		$this->tmp->assign('user_data',$user_data);
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//增加渠道方
	function channel_add()
	{
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//保存渠道
	function channel_save()
	{
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$data['c_time'] = time();
		$data['channel_name']=$post['channel_name'];
		$data['channel_phone']=$post['channel_phone'];
		$res = $this->model->saveChannel($data,$id);
		if($res)
		{
			$da['c_time']=$data['c_time'];
			$da['name']=$post['name'];
			$da['password']=MD5(MD5($post['password']));
			if(!$id){
				$da['channel_id']=$res;
			}else{
				$da['channel_id']=$id;
			}
			$this->model->saveChannelUser($da);
			$url = "index.php?m=admin&o=ticket&t=channel";
			MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
		}
		
	}
//删除渠道方
	function channel_del()
	{
		$get=$this->GVar->fget;
		$resources_data = $this->model->delChannelById($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=ticket&t=channel',1,_lang_save_fail,'index.php?m=admin&o=ticket&t=channel',2);
	}
//渠道 应用列表
	function channel_app_list()
	{
		$id = $this->GVar->fget['id'];
		$applist = $this->model->getChannelApp($id);
		foreach($applist as $k=>$v)
		{
			$bus_name = $this->model->getBusinessName($v['author_business_id']);
			$str = '';
			foreach($bus_name as $key=>$value)
			{
			    $str .=$value['business_name'].',';
			}
			$str = substr($str,0,-1);
			$applist[$k]['business_name']=$str;
		}
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign('id',$id);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_app_list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//添加 渠道  应用 
	function channel_app_add()
	{
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_app_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//保存渠道 应用
	function channel_app_save()
	{
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$post['c_time'] = time();
		$res = $this->model->saveAppChannel($post,$id);
		$url = "index.php?m=admin&o=ticket&t=channel_app_list&id=".$post['channel_id'];
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
	}
//编辑 渠道 应用
	function channel_app_edit()
	{
		$id = $this->GVar->get['id'];
		$group = $this->model->getAllChannel();
		$data = $this->model->getChannelAppById($id);
		$this->tmp->assign('data',$data);
		$this->tmp->assign('group',$group);
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_app_form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}

//删除渠道方 应用
	function channel_app_del()
	{
		$get=$this->GVar->fget;
		$resources_data = $this->model->delChannelAppById($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=ticket&t=channel_app_list&id='.$this->GVar->fget['channel_id'],1,_lang_save_fail,'index.php?m=admin&o=ticket&t=channel_app_list&id='.$this->GVar->fget['channel_id'],2);
	}

//渠道方  应用 绑定商户
	function binding_business()
	{
		/*$id = $this->GVar->get['id'];
		$channel_app_data = $this->model->getChannelAppById($id);
		$app_data = explode(',',$channel_app_data['author_business_id']);
		$binding_use = $this->model->binding_use($channel_app_data['author_business_id']);
		$this->tmp->assign('binding_use',$binding_use);
		$this->tmp->assign('app_data',$app_data);
		$this->tmp->assign('channel_id',$channel_app_data['channel_id']);
		$this->tmp->assign("main_show_block","admin/com_ticket/binding_business.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");*/
		$id = $this->GVar->get['id'];
		$channel_app_data = $this->model->getChannelAppById($id);
		$app_data = explode(',',$channel_app_data['author_business_id']);
		$binding_use = $this->model->binding_use($channel_app_data['channel_id']);
		$this->tmp->assign('binding_use',$binding_use);
		$this->tmp->assign('app_data',$app_data);
		$this->tmp->assign('channel_id',$channel_app_data['channel_id']);
		$this->tmp->assign("main_show_block","admin/com_ticket/binding_business.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
//绑定商户 保存信息 
	function binding_business_save()
	{
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		
		//把商户标记为没有被绑定 start
		//$author_business_id_data = $this->model->getChannelAppById($id);
		//$data['author_status']=1;
		//$this->model->saveBusinessStatus($author_business_id_data,$data);
		//end

		$post['author_business_id']=implode(',',$post['author_business_id']);
		$post['c_time'] = time();
		$res = $this->model->saveBudingBusiness($post,$id);
		if($res){
			//商户标注为选中状态  start
			$data['author_status'] = 2;
			$this->model->saveBusinessStatus($post['author_business_id'],$data);
			//end
			$url = "index.php?m=admin&o=ticket&t=channel_app_list&id=".$post['channel_id'];
			MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,$url,3);
		}
		
	}










	//应用列表（渠道列表）
	function applist()
	{
		$applist = $this->model->getAppList();
		$this->tmp->assign('applist',$applist);
		$this->tmp->assign("main_show_block","admin/com_app/app-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加应用
	function addapp(){
		//获取渠道方列表
		$channel_data = $this->model->getChannelListtwo();
		//获取资源方列表
		$resources_data = $this->model->getResourcesList();
		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/app-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//编辑应用
	function editapp(){
		$id = $this->GVar->get['id'];
		//获取资源方列表
		$channel_data = $this->model->getChannelListtwo();
		//获取渠道方列表
		$resources_data = $this->model->getResourcesList();
		//获取应用数据
		$data = $this->model->getChannerData($id);

		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign('data',$data);
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/app-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//生成key
	function createKey(){
		$str = time().rand(10000,99999);
		$key = md5($str);
		echo $key;
	}

	//保存应用
	function saveApp(){
		$post=$this->GVar->fpost;
		$id = $this->GVar->fget['id'];
		$post['c_time'] = time();
		$res = $this->model->saveApp($post,$id);
		$url = "index.php?m=admin&o=app&t=applist";
		MessageClass::ShowMessage(_lang_save_success,$url,$res,_lang_save_fail,null,3);
		
	}
	//查看报表
	function appStatistics(){
		$key = $this->GVar->fget['key'];
		$type = $this->GVar->fget['type'];
		//全国分布
		$datas = $this->model->getChannelLogGroupBy('`key`="'.$key.'"','city');
		$unknow_city_num = 0;
		$all_num = 0;
		foreach ($datas as $k=>$v){
			if (!$v['city']) {
				$unknow_city_num+=$v['num'];
				unset($datas[$k]);
			}
			$all_num+=$v['num'];
		}
		$this->tmp->assign('unknow_city_num',$unknow_city_num);
		$this->tmp->assign('all_num',$all_num);
		$this->tmp->assign('datas',$datas);
		//机型分布
		$mobile_datas = $this->model->getChannelLogGroupBy('`key`="'.$key.'"','mobile_system');
		$this->tmp->assign('mobile_datas',$mobile_datas);
		//时间详细数据
		$time_datas = $this->model->getChannelLog('`key`="'.$key.'"');

		foreach ($time_datas as $v){
			$time_arr[date('Y/m/d',$v['time'])] ++; 
		}
//		foreach ($time_arr as $k=>$v){
//			$time[] = $k;
//			$time_num[] = $v;  
//		}
//
//		$this->tmp->assign('time',$time);
//		$this->tmp->assign('time_num',$time_num);
		$this->tmp->assign('time_arr',$time_arr);
		
		
		$this->tmp->assign("main_show_block","admin/com_app/app-statistics.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//资源管理方列表
	function resources(){
		$resources_data = $this->model->getResourcesList();
		$this->tmp->assign('resources_data',$resources_data);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_app/app-resources-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加资源方
	function addresources(){
		$this->tmp->assign("main_show_block","admin/com_app/resources-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//保存资源方信息
	function saveresources(){
		//$get=$this->GVar->fget;
		$post=$this->GVar->fpost;
		if($post['username'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方名称',$url,$re,_lang_save_fail,$url,2);
		}
		if($post['url'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方URL',$url,$re,_lang_save_fail,$url,2);
		}
		$data['resources_name']=$post['username'];
		$data['resources_url']=$post['url'];
		$data['company']=$post['company'];
		$data['contact_name']=$post['contact_name'];
		$data['contact_phone']=$post['contact_phone'];		
		$data['status']=$post['status'];
		if(!$post['id'])
		{
			$data['c_time']=time();
			$this->model->saveResources($data);
		MessageClass::ShowMessage('添加成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}else{
			$this->model->editResources($data,$post['id']);
		MessageClass::ShowMessage('编辑成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}
		
	}
	//编辑资源方信息
	function editresources(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->getResources($get['id']);
		$this->tmp->assign("data",$resources_data);
		$this->tmp->assign("main_show_block","admin/com_app/resources-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//删除资源方信息
	function delresources(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->delResources($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=app&t=resources',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
	}
	
	//渠道方列表
	function channelsite()
	{
		$get=$this->GVar->fget;
		$channel_data = $this->model->getChannelList();
		$this->tmp->assign('channel_data',$channel_data);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_channel/channel-list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//增加渠道方
	function addchannel(){
		$this->tmp->assign("main_show_block","admin/com_channel/channel-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//保存渠道方信息
	function savechannel(){
		//$get=$this->GVar->fget;
		$post=$this->GVar->fpost;
		if($post['username'] == null){
			$url = "index.php?m=admin&o=app&t=addresources";
			$re=1;
			MessageClass::ShowMessage('请填写资源方名称',$url,$re,_lang_save_fail,$url,2);
		}
		$data['channel_name']=$post['username'];
		$data['company']=$post['company'];
		$data['contact_name']=$post['contact_name'];
		$data['contact_phone']=$post['contact_phone'];		
		$data['status']=$post['status'];
		if(!$post['id'])
		{
			$data['c_time']=time();
			$this->model->saveChannel($data);
		MessageClass::ShowMessage('添加成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}else{
			$this->model->editChannel($data,$post['id']);
		MessageClass::ShowMessage('编辑成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=resources',2);
		}		
	}
	//编辑渠道方信息
	function editchannel(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->getChannel($get['id']);
		$this->tmp->assign("data",$resources_data);
		$this->tmp->assign("main_show_block","admin/com_channel/channel-form.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//删除渠道方信息
	function delchannel(){
		$get=$this->GVar->fget;
		$resources_data = $this->model->delChannel($get['id']);
		MessageClass::ShowMessage('删除成功','index.php?m=admin&o=app&t=channelsite',1,_lang_save_fail,'index.php?m=admin&o=app&t=channelsite',2);
	}
	//接口日志
	function apiLog(){
		$datas = $this->model->getApiLogList();
		$this->tmp->assign('datas',$datas);
		
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		
		$this->tmp->assign("main_show_block","admin/com_ticket/api_log_list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
		
	}
	//趋势分析
	function chat(){
		$get = $this->GVar->fget;
		$channel_data = $this->model->getAllChannelNoPage();
		$this->tmp->assign('channel_data',$channel_data);
		if ($get['channel_id']) {
			$shop_id = $this->model->binding_use($get['channel_id']);
		
			
			foreach ($shop_id as $v){
				$ids[] = $v['id'];
			}
		
			
			$where[] = ' shop_id in('.implode(',',$ids).')';
		}
		if ($get['day']){
			$day = $get['day'];
		}else{
			$day = 7;
		}
		
		$begin_time = time()-3600*$day*24;
		$end_time = time();
		$where[] = ' (c_time >= '.$begin_time.' and c_time<='.$end_time.')'.' or '.' (open_time >= '.$begin_time.' and open_time<='.$end_time.')'.' or '.' (revoke_time >= '.$begin_time.' and revoke_time<='.$end_time.')';
		//$where[] = 'status=2';
		$ticket_data = $this->model->getTicketList($where);
	
		
		$ticket_arr = array(
			'待开票'=>array(),
			'已开票'=>array(),
			'已冲红'=>array(),
		);
		foreach ($ticket_data as $k=>$v){
			
			if ($v['status']==1) {
				$ticket_arr['待开票'][date('m-d',$v['c_time'])]['count']+=1;
				$ticket_arr['待开票'][date('m-d',$v['c_time'])]['price']+=$v['price'];
			}
			
			if ($v['status']==2) {
				$ticket_arr['已开票'][date('m-d',$v['open_time'])]['count']+=1;
				$ticket_arr['已开票'][date('m-d',$v['open_time'])]['price']+=$v['price'];
			}
			
			if ($v['status']==3) {
				$ticket_arr['已冲红'][date('m-d',$v['revoke_time'])]['count']+=1;
				$ticket_arr['已冲红'][date('m-d',$v['revoke_time'])]['price']+=$v['price'];
			}
		}
		
		for ($i=$day;$i>=0;$i--){
			$date_arr[] = "'".date("m-d",time()-3600*$i*24)."'";
		}
		
		
		$this->tmp->assign('date',implode(',',$date_arr));
		foreach ($ticket_arr as $k=>$v){
			for ($i=$day;$i>=0;$i--){
				$date = date("m-d",time()-3600*$i*24);
				if ($v[$date]) {
					$ticket[$k][$date] = $v[$date]['count'];
					$ticket_price[$k][$date] = $v[$date]['price'];
				}else{
					$ticket[$k][$date] = 0;
					$ticket_price[$k][$date] = 0;
				}
				
			}
		}
		foreach ($ticket as $k=>$v){
			$ticket[$k] = implode(',',$v);
		}
		foreach ($ticket_price as $k=>$v){
			$ticket_price[$k] = implode(',',$v);
		}
		
	    
		
		$begin_time = time()-3600*$day*24;
		$end_time = time();
		$this->tmp->assign('ticket',$ticket);
		$this->tmp->assign('ticket_price',$ticket_price);
		$where = array();
		if ($this->GVar->fget['channel_id']) {
			$where[] = ' channel_id in('.implode(',',$ids).')';
		}
		$where[] = ' timestamp>='.$begin_time.' and timestamp<='.$end_time;
		$log_data = $this->model->getApiLog($where);
		foreach ($log_data as $v){
			if ($v['type']){
				$log_type[$this->apiTypeName($v['type'])]+=1;
				$log[$this->apiTypeName($v['type'])][date('m-d',$v['timestamp'])]+=1;
			}else{
				$log_type['未知']+=1;
				$log[$this->apiTypeName($v['type'])][date('m-d',$v['timestamp'])]+=1;
			}
			
		}
		for ($i=$day;$i>=0;$i--){
			$log_date_arr[] = "'".date("m-d",time()-3600*$i*24)."'";
		}
		
		
		$this->tmp->assign('log_date',implode(',',$log_date_arr));
		
		foreach ($log as $k=>$v){
			for ($i=$day;$i>=0;$i--){
				$date = date("m-d",time()-3600*$i*24);
				if ($v[$date]) {
					$log[$k][$date] = $v[$date];
					
				}else{
					$log[$k][$date] = 0;
					
				}
				
			}
		}
		foreach ($log as $k=>$v){
			$log[$k] = implode(',',$v);
		}
		
		$this->tmp->assign("log_type",$log_type);
		$this->tmp->assign("log",$log);
		
		
		
		$this->tmp->assign("main_show_block","admin/com_ticket/chat.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
	//接口类型中文名
	function apiTypeName($type){
		$array = array(
			'queryTicket'=>'查询发票',
			'getTicketInfo'=>'发票信息',
			'openTicket'=>'开票',
			'revoke'=>'冲红',
			'createTicket'=>'预下单',
			'querybillingcount'=>'开票统计',
			'queryBillLeftCount'=>'余票查询',
			//'queryTicket'=>'查询发票',
		);
		if ($array[$type]) {
			return $array[$type];
		}else{
			return '未知';
		}
		//return $array[$type];
	}
	//应用列表
	function app(){
		$applist = $this->model->getChannelApp();
		
		$this->tmp->assign("applist",$applist);
		$this->tmp->assign("page_menu",$this->model->page_obj->getPageMenu());
		$this->tmp->assign("main_show_block","admin/com_ticket/channel_app_list.html");
		$this->createMenu();
		//把区块发送到前
		$this->dp("admin/index");
	}
}

?>